﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCenter.Models;
using UCenter.DataProvider;
using UCenter.Core.Repository;

namespace UCenter.SQLServerProvider
{
    public class MessageRepository : SqlRepositoryBase<Message>, IMessageRepository
    {       

        /// <summary>
        /// get message of inbox
        /// </summary>
        /// <param name="userID">user id</param>
        /// <returns></returns>
        public List<Message> GetInBoxMessages(int userID) {
            using (RepositoryContext db = new RepositoryContext()) {
                var q = from m in db.GetTable<Message>().OrderByDescending(msg => msg.SentDate)
                        where m.ToUserID == userID
                        select m;
                return q.ToList<Message>();
            }
        }
        /// <summary>
        /// change status of message
        /// </summary>
        /// <param name="msgID">message id</param>
        /// <param name="status">value of status</param>
        public  void ChangeStatus(int msgID, bool isNew) {
            using (RepositoryContext db = new RepositoryContext()) {
                var q = (from m in db.GetTable<Message>()
                        where m.ID == msgID
                        select m).SingleOrDefault<Message>();
                q.IsNew = isNew;
                db.SubmitChanges();
            }
        }
        /// <summary>
        /// delete message 
        /// </summary>
        /// <param name="msgID">message id</param>
        public void Delete(int msgID) {
            using (RepositoryContext db = new RepositoryContext()) {
                var q = (from m in db.GetTable<Message>()
                         where m.ID == msgID
                         select m).SingleOrDefault<Message>();
                db.GetTable<Message>().DeleteOnSubmit(q);
                db.SubmitChanges();
            }
        }
    }
}
